<?php
/**
 * 处理卖出订单.
 * User: madong
 * Date: 2018/10/7
 * Time: 3:35 AM
 */

namespace app\api\controller\jiaoyi;
use think\Config;
use app\api\model\Jiaoyi;
use app\api\model\JiaoyiUinfo;
use app\api\model\User;
use think\Controller;
use think\Db;
use think\Request;

class Changwaisell extends Controller
{
    private $Order_linit_time = 15; // 订单自动取消时间（单位/分钟）
    //处理卖出订单
    public function Order_sell_dispose (){

        $Uinfo = new JiaoyiUinfo();

        $Main_Order_Obj = new Jiaoyi();

        $res = Request::instance();

        $Max = $Main_Order_Obj->where('ordernumber',$this->Jiemi($res->param('ordernumber')))->value('da');

        $Min = $Main_Order_Obj->where('ordernumber',$this->Jiemi($res->param('ordernumber')))->value('xiao');

        $U_info_all = $Uinfo->where('uname',$this->Jiemi($res->param('sellname')))->find();//卖出账户全部信息-当我登录的用户信息

        $O_info_all = $Uinfo->where('uname',$this->Jiemi($res->param('buyname')))->find();//买方账户全部信息-求购方的账户信息

        $U_balance = $Uinfo->where('uname',$this->Jiemi($res->param('sellname')))->value($res->param('bizhong'));//当前卖出账户的余额-当前登录账户

        $O_balance = $Uinfo->where('uname',$this->Jiemi($res->param('buyname')))->value($res->param('bizhong'));//求购方的账户余额

        $U_buy_num = ['cny'=>$res->param('cnynum'),'coin'=>$res->param('coinnum')];//购买的数量(包括人民币和货币)

        $U_name_O_name = ['buy_name'=>$this->Jiemi($res->param('buyname')),'sell_name'=>$this->Jiemi($res->param('sellname'))];//获取买方和卖方的账号

        $new_time = time();

        if ($res->isPost()){

            if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/',$U_buy_num['coin'])){echo json_encode(['zt'=>0,'msg'=>'请输入正确的数量']);die();}

            if (!$U_buy_num['coin']){echo json_encode(['zt'=>0,'msg'=>'数量不能为空']);die();}

            if ($U_buy_num['coin'] < $Min || $U_buy_num['coin'] > $Max){echo json_encode(['zt'=>0,'msg'=>'请认真检查交易限额']);die();}

            if ($U_buy_num['coin']>$U_balance){echo json_encode(['zt'=>0,'msg'=>'卖出数量不能超过您账户余额']);die();}

            if ($U_name_O_name['buy_name'] == $U_name_O_name['sell_name']){echo json_encode(['zt'=>0,'msg'=>'不能购买自己的订单!']);die();}

            if (!$res->param('zhifu')){echo json_encode(['zt'=>0,'msg'=>'请选择推荐收款方式']);die();}

            Db::startTrans();

            try{

                Db::name('jiaoyibuylog')->insert([

                    'uname' => $U_name_O_name['sell_name'],//卖出方账号

                    'uid' => $U_info_all['uid'],//卖出方ID

                    'unickname' => $U_info_all['unickname'],//卖出方昵称

                    'utruename' => $U_info_all['truename'],//卖出方真实姓名

                    'ucnynum' => $U_buy_num['cny'],//卖方提交过来的人民币数量

                    'ucoinnum' => $U_buy_num['coin'],//卖方提交过来的货币数量

                    'ubizhong' => $res->param('bizhong'),//卖出的币种类型

                    'ufukuanfs' => $res->param('zhifu'),//卖方推荐的收款方式

                    'uordernumber' => $this->Jiemi($res->param('ordernumber')),//卖方卖出的广告订单编号(求购方的广告单号)

                    'uorderzt' => 1,//卖出状态  0 买入  1 卖出

                    'ubeizhu' => $U_name_O_name['sell_name'].'卖出入'.$U_buy_num['coin'].'个'.$res->param('bizhong').'收到人民币'.$U_buy_num['cny'],//当前订单的说明

                    'oid' => $O_info_all['uid'],//买方ID

                    'oname' => $U_name_O_name['buy_name'],//买方账号

                    'onickname' => $O_info_all['unickname'],//买方昵称

                    'otruename' => $O_info_all['truename'],//买方真实姓名

                    'ouordernumber' => $this->Jiemi($res->param('ordernumber')),//买方广告订单编号和卖出方uordernumber一样。整理数据库的时候可能要删除

                    'oorderzt' => 0,//当前订单状态 0 未付款 1已付款 2已释放 后面还有暂定

                    'zfbzh' => $U_info_all['zhifubaoaccount'],//卖出方支付宝账户

                    'zfbimg' => $U_info_all['zhifubaoimg'],//卖出方支付宝收款码

                    'wxzh' => $U_info_all['weixinaccount'],//卖出方微信账号

                    'wximg' => $U_info_all['weixinimg'],//卖出方微信收款码

                    'ylzh' => $U_info_all['bankname'],//卖出方银行卡号

                    'ylname' => $U_info_all['bankkaihu'],//卖出方银行名称

                    'yladdr' => $U_info_all['bankkaihuaddr'],//卖出方开户行地址

                    'time' => $new_time,//订单生成时间

                    'ordernumber' => strtoupper($res->param('bizhong')).'-'.strtoupper(md5($this->Jiemi($res->param('sell_name')).$new_time)),//当前订单的编号

                    'dqsj' => $this->Order_linit_time,//当前订单的取消时间

                    'chengjiaojiage' => $res->param('chengjiaojiage')//成交价格

                ]);

                Db::name('jiaoyi')->where('ordernumber',$this->Jiemi($res->param('ordernumber')))->setInc('wfkdd',1);//给求购订单未付款+1

                Db::name('jiaoyi_uinfo')->where('uname',$U_name_O_name['sell_name'])->setDec($res->param('bizhong'),$U_buy_num['coin']);//卖出的用户减去相应的货币数量

                Db::name('jiaoyi_uinfo')->where('uname',$U_name_O_name['sell_name'])->setInc('d'.$res->param('bizhong'),$U_buy_num['coin']);//卖出的冻结加上相应的货币数量

                //冻结信息添加冻结表
                Db::name('jiaoyidj')->insert([

                    'uid' => $U_info_all['uid'],//被冻结的用户ID  卖方

                    'uname' => $U_name_O_name['sell_name'],//被冻结的用户ID  卖方

                    'oid' => $O_info_all['uid'],//对方id 买方

                    'oname' => $U_name_O_name['buy_name'],//对方账户 买方

                    'bizhong' => $res->param('bizhong'),//冻结币种

                    'ddhao' => strtoupper($res->param('bizhong')).'-'.strtoupper(md5($res->param('uname').$new_time)),//当前订单编号

                    'oddhao' => $this->Jiemi($res->param('ordernumber')),//对方订单编号

                    'djnum' => $U_buy_num['coin'],//冻结数量

                    'djcnynum' => $U_buy_num['cny'],//人民币数量

                    'djqnum' => $U_balance,//冻结钱余额

                    'djhnum' => $U_balance-$U_buy_num['coin'],//冻结后余额

                    'djtype' => 1,//0买入 1卖出

                    'djnote' => $U_name_O_name['sell_name'].'卖给用户'.$U_name_O_name['buy_name'].$res->param('bizhong').'数量'.$U_buy_num['coin'],//说明

                    'djzt' => 0,//D冻结状态 0冻结  1释放

                    'time' => $new_time//冻结时间

                ]);

                Db::commit();

                echo json_encode(['zt'=>1,'msg'=>'下单成功,等待对方付款']);

            } catch (\Exception $e) {

                echo json_encode(['zt'=>0,'msg'=>'下单失败,稍后重试']);

                Db::rollback();
            }

        }else{

            echo json_encode(['zt'=>2,'msg'=>'error']);

        }

    }
    //数据解密
    private function Jiemi($data){

        $jiami_data = openssl_decrypt(base64_decode($data,false),"AES-128-CBC",Config::get('Key'),OPENSSL_RAW_DATA,Config::get('IV'));

        return $jiami_data;

    }
    //数据加密
    private function Jiami($data){

        $jiemi_data =  base64_encode(openssl_encrypt($data,"AES-128-CBC",Config::get('Key'),OPENSSL_RAW_DATA,Config::get('IV')));

        return $jiemi_data;

    }

}